package it.scambiocorse.rowmapper;

import it.scambiocorse.model.ClienteType;
import it.scambiocorse.utils.Utils;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.RowMapper;

public class ClienteRowMapper implements RowMapper {
	final Logger logger = Logger.getLogger(getClass());

  public Object mapRow(ResultSet rs, int row) throws SQLException {
  	String metodo ="mapRow(...)";
  	
    ClienteType c = null;
    
    try {
    	c = new ClienteType();
    	if (row > -1) { // PROVENGO DALLA TABELLA
    		c.setIdCliente(rs.getInt("id_cliente"));
	    	c.setDescNome(Utils.isValid(rs.getString("desc_nome")) ? rs.getString("desc_nome") : "");
	    	c.setEmail(Utils.isValid(rs.getString("email")) ? rs.getString("email") : "");
	    	c.setIdUser(rs.getInt("id_user_inse"));
	    	c.setTelefono(Utils.isValid(rs.getString("tel")) ? rs.getString("tel") : "");
    	} else {
        	// PROVENGO DALLA VIEW (ServizioRowMapper)
    		c.setIdCliente(rs.getInt("id_cliente"));
        	c.setIdUser(rs.getInt("id_user_inse_cliente"));
        	c.setDescNome(Utils.isValid(rs.getString("nome_cliente")) ? rs.getString("nome_cliente") : "");
        	c.setTelefono(Utils.isValid(rs.getString("tel_cliente")) ? rs.getString("tel_cliente") : "");
        	c.setEmail(Utils.isValid(rs.getString("email_cliente")) ? rs.getString("email_cliente") : "");

    	}
	} catch (SQLException e) {
		if (e.getSQLState().equals("S0022")) {
			logger.debug("Colonna non trovata, mappaggio non necessario");
		} else {
			throw e;
		}
	}
    	

    
    return c;
  }
}
